package component.fulltextsearch.index;

import java.util.Date;

import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreRangeQuery;
import org.apache.lucene.search.Query;

import component.fulltextsearch.constant.SearchConstant;
import component.fulltextsearch.util.AnalyzerUtil;

public abstract class SearcherQueryBuilder {
	
	/**
	 * 构造查询器
	 * 构建后的条件：( 搜索字段1 like 值1 or 搜索字段2 like 值2 or ...) and ( 搜索字段1 not like 值1 and 搜索字段2 not like 值2 ) and (开始日期 结束日期)
	 * @param queryExpression 查询表达式
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @param field 搜索字段 为null表示搜索所有字段
	 * @return
	 * @throws ParseException 表达式解析异常
	 */
	public static Query builderQuery(String queryExpression, Date startDate,Date endDate, String field) throws ParseException {
		BooleanQuery bQuery = new BooleanQuery();
		if (field == null || "".equals(field.trim())) {			
			String[] expressions = queryExpression.split(" ");
			StringBuffer aExpression = new StringBuffer();
			StringBuffer bExpression = new StringBuffer();
			for (int i = 0; i < expressions.length; i++) {
				String expression = expressions[i];
				if (expression == null || "".equals(expression.trim())) continue;
				if(expression.trim().startsWith("-")){//不包含的条件
					bExpression.append(" ").append(expression);
				}else{//包含的条件
					aExpression.append(" ").append(expression);
				}
			}	
			if(aExpression.length()>0){
				BooleanQuery aBooleanQuery = new BooleanQuery();
				for (int i = 0; i < SearchConstant.SEARCHER_FIELD_NAMES.length; i ++) {
					QueryParser qp = new QueryParser(SearchConstant.SEARCHER_FIELD_NAMES[i], AnalyzerUtil.getAnalyzer());
					Query q = qp.parse(aExpression.toString());
					if ((!(q instanceof BooleanQuery) || ((BooleanQuery)q).getClauses().length>0)) {
						aBooleanQuery.add(q,  BooleanClause.Occur.SHOULD);
					}			
				}	
				bQuery.add(aBooleanQuery,BooleanClause.Occur.SHOULD);
			}
			if(bExpression.length()>0){
				BooleanQuery bBooleanQuery = new BooleanQuery();
				for (int i = 0; i < SearchConstant.SEARCHER_FIELD_NAMES.length; i ++) {
					QueryParser qp = new QueryParser(SearchConstant.SEARCHER_FIELD_NAMES[i], AnalyzerUtil.getAnalyzer());
					Query q = qp.parse(bExpression.toString());
					if ((!(q instanceof BooleanQuery) || ((BooleanQuery)q).getClauses().length>0)) {
						bBooleanQuery.add(q,  BooleanClause.Occur.MUST_NOT);
					}			
				}
				bQuery.add(bBooleanQuery,BooleanClause.Occur.MUST);	
			}
		} else {//只根据指定搜索字段进行查询
			QueryParser qp = new QueryParser(field, AnalyzerUtil.getAnalyzer());
			Query q = qp.parse(queryExpression);
			if ((!(q instanceof BooleanQuery) || ((BooleanQuery)q).getClauses().length>0)) {
				bQuery.add(q,  BooleanClause.Occur.SHOULD);
			}
		}
		if (startDate != null || endDate != null) {
			String start = null; 
			String end = null;
			if (startDate != null) start = SearchConstant.DATA_FORMAT.format(startDate);
			if (endDate != null) end = SearchConstant.DATA_FORMAT.format(endDate);			
			ConstantScoreRangeQuery rangeQuery = new ConstantScoreRangeQuery(SearchConstant.NAME_CREATETIME, start, end, true, true);
			BooleanQuery newBQuery = new BooleanQuery();
			newBQuery.add(bQuery,BooleanClause.Occur.MUST);
			newBQuery.add(rangeQuery, BooleanClause.Occur.MUST);
			return newBQuery;
		}
		return bQuery;
	}
}
